package actionHandlers;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JColorChooser;
import javax.swing.JTable;
import javax.swing.JWindow;

import palette.CanvasManager;
import palette.CanvasWindow;
import palette.CurrentColor;
import cuShapes.Arrow;
import cuShapes.Box;
import cuShapes.Ellipse;
import cuShapes.PQShape;

public class PropTableColorListener extends MouseAdapter{
	
	public void mouseClicked(MouseEvent e) {
    	//System.out.println("mouseClicked on properties table occured.");
    	PQShape currShape = ((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCurrentShape();
		JTable table = currShape.getShapeTable().getCurrentTable();
		Color tempColor;
		
		
        if (e.getClickCount() == 2)
        {
        	//System.out.println("doubleClick on color cell occured.");
            Point p = e.getPoint();
            int row = table.rowAtPoint(p);
            int column = table.columnAtPoint(p); // This is the view column!

            // only looking for user editing the color
            if(currShape instanceof Arrow) {
            	if(row == 1 && column == 1) {
            		Color c = JColorChooser.showDialog(new JWindow(), "Choose Color", 
            				CurrentColor.getInstance());
            		if (c==null) {
            			c = CurrentColor.getInstance();
            		}
            		else {
            			tempColor = CurrentColor.getInstance();
            			CurrentColor.getInstance().setColor(c);
            			currShape.setColor((Color) CurrentColor.getInstance());
            			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
            			currShape.getShapeTable().removeTableModelListener();
            			currShape.getShapeTable().setProperties(currShape);
            			currShape.getShapeTable().addTableModelListener();
            			CurrentColor.getInstance().setColor(tempColor);
            		}
            	}
            } else if (currShape instanceof Box || currShape instanceof Ellipse) {
            	if(row == 5 && column == 1) {
            		Color c = JColorChooser.showDialog(new JWindow(), "Choose Color", 
            				CurrentColor.getInstance());
            		if (c==null) {
            			c = CurrentColor.getInstance();
            		}
            		else {
            			tempColor = CurrentColor.getInstance();
            			CurrentColor.getInstance().setColor(c);
            			currShape.setColor((Color) CurrentColor.getInstance());
            			((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
            			currShape.getShapeTable().removeTableModelListener();
            			currShape.getShapeTable().setProperties(currShape);
            			currShape.getShapeTable().addTableModelListener();
            			CurrentColor.getInstance().setColor(tempColor);
            		}
            	}
            }
        }
    }
}
